<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/27 0027
 * Time: 10:23
 */

namespace Admin\Controller;



class AreaController extends CommonController
{
    public function index(){

        $db = M('region_list');
        $cate = $this->getCateTree($db,$id=0,$pid='parent_region_id',$zid='region_id');
        $this->assign('cate',$cate);
        $this->display();
    }

    public function add(){

        if(IS_POST){
            $data = I('post.');

            if(!empty($data['province']) and !empty($data['city'])){
                $map['parent_region_id'] = $data['city'];
            }elseif(!empty($data['province']) and empty($data['city'])){
                $map['parent_region_id'] = $data['province'];
            }elseif(empty($data['province']) and empty($data['city'])){
                $map['parent_region_id'] = 0;
            }
            $map['region_name']= $data['region_name'];
            $map['region_code']= $data['region_code'];

            $rel = M('region_list')->add($map);

            if($rel){
                die;
            }
        }


        /* $db = M('region_list');
         $cate = $this->getCateTree($db,$id=0,$pid='parent_region_id',$zid='region_id');*/
        $cate = M('region_list')->where(array('parent_region_id'=>1))->select();
        $this->assign('cate',$cate);
        $this->display();
    }

    //下级分类（市）
    public function subordinate(){
        $id = I('post.id');
        $list = M('region_list')->where(array('parent_region_id'=>$id))->select();
        die(json_encode(array('state'=>'200','rel'=>$list)));
    }

}